package com.android.ailaoshan.location;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;

import com.android.ailaoshan.common.BaseApplication;

import java.util.HashMap;

/**
 * 获取最后一个已知位置
 */
public class LastLocation {

    public static HashMap<String,Double> getLocation(){

        Context mContext = BaseApplication.getInstance().getbContext();

        LocationManager locationManager = (LocationManager)
                mContext.getSystemService(Context.LOCATION_SERVICE);

        HashMap<String,Double> locationMap = new HashMap<>();

        if(locationManager != null){
            String gpsProvider = LocationManager.GPS_PROVIDER;
            String netProvider = LocationManager.NETWORK_PROVIDER;

            Location location;

            location = locationManager.getLastKnownLocation(gpsProvider);
            // GPS不能获取到位置，尝试网络获取位置
            if(location == null){
                location = locationManager.getLastKnownLocation(netProvider);
            }

            if(location != null){
                locationMap.put("lat",location.getLatitude());
                locationMap.put("lng",location.getLongitude());

                return locationMap;
            }else{
                return null;
            }

        }
        // 不能获取到LocationManager，返回null
        else{
            return null;
        }
    }
}
